/**
 * @System abcs-aspchain
 * @Description: Copyright: Copyright (c) 2018
 * Company: Aspire Technologies
 * @author zhongzifeng
 * @date 2018/11/29 16:05
 * @version V1.0
 */
package com.asp.aspchain.core.util;

/**
 * Url操作工具类
 * @author zhongzifeng
 * @date 2018/11/29 16:05
 *
 */
public class UrlUtil {

    /**
     * 截取域名或ip+port
     * @param url 截取的url
     * @param isGetIp 是否只截取ip地址（仅供ip+port）
     * @return
     */
    public static String subHostName(String url, boolean isGetIp){
        String result = "";
        if("".equals(url)){
            return result;
        }
        int headerIndex = url.indexOf("://");
        if(headerIndex < 0) {
            headerIndex = 0;
        }
        headerIndex += 3;
        int lastIndex = url.indexOf("/",headerIndex);
        if(lastIndex<0){
            result = url.substring(headerIndex);
        }else{
            result = url.substring(headerIndex,lastIndex);
        }
        if(isGetIp){
            result = subIp(result);
        }
        return result;
    }


    /**
     * 截取ip地址
     * @param url
     * @return
     */
    private static String subIp(String url){
        if("".equals(url)){
            return "";
        }
        int lastIndex = url.indexOf(":");
        if(lastIndex<0){
            return url;
        }
        return url.substring(0,lastIndex);
    }

}
